﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text.RegularExpressions;

namespace EcSalseManage.EcSalse
{
    public partial class Report_Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataAccess.Operate.DA_Report_Search_Inherit Report_Search_Inherit = new DataAccess.Operate.DA_Report_Search_Inherit();
                DataSet ds = Report_Search_Inherit.FindLibrary();
                Common.Common_Function.DropDownListBind(ddlExchangeType, ds.Tables[0], "Report_ExchangeType_ID", "Report_ExchangeType_Name", "", "--请选择成交类型--");
                this.ddlExchangeType.Attributes.Add("onchange", "view();");
                this.ddlExchangeType.Items.RemoveAt(11); //移除‘项目管理’项
            }
            else
            {
                if (this.ddlExchangeType.Items[1].Selected == true)
                    this.div1.Style["display"] = "block";
                else
                    this.div1.Style["display"] = "none";
                if (this.ddlExchangeType.Items[2].Selected == true)
                    this.div2.Style["display"] = "block";
                else
                    this.div2.Style["display"] = "none";
            }
           
        }





        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string sSql = "select ";
            string ExchangeTypeID = this.ddlExchangeType.SelectedValue.ToString();
            string ExchangeDate = this.txtExchangeDate.Text.Trim();
            string ReportNO = this.txtReportNO.Text.Trim();
            if (ExchangeTypeID == "1")
            {
                CheckBoxList cbl = Page.FindControl("CheckBoxList1") as CheckBoxList;
                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    if (cbl.Items[i].Selected == true)
                    {
                        if (cbl.Items[i].Value.ToString() == "field_0001")
                        { sSql = sSql + "(select Project_Name from t_Project where Project_ID=t_Report_Plan.Report_Plan_ProjectID) as 项目名称,"; }
                        if (cbl.Items[i].Value.ToString() == "field_0002")
                        { sSql = sSql + "Report_Plan_ReportNO as 成交报告编号,"; }
                        if (cbl.Items[i].Value.ToString() == "field_0003")
                        { sSql = sSql + "Report_Plan_ExchangeDate as 成交日期,"; }
                        if (cbl.Items[i].Value.ToString() == "field_0004")
                        { sSql = sSql + "Report_Plan_Comm as 策划费佣金总额,"; }
                        if (cbl.Items[i].Value.ToString() == "field_0005")
                        { sSql = sSql + "Report_Plan_ProjectDep as 项目部占额,"; }
                        if (cbl.Items[i].Value.ToString() == "field_0006")
                        { sSql = sSql + "Report_Plan_Referral as 转介部门占额,"; }
                        if (cbl.Items[i].Value.ToString() == "field_0007")
                        { sSql = sSql + "Report_Plan_PayRemark as 支付策划费摘要,"; }
                        if (cbl.Items[i].Value.ToString() == "field_0008")
                        { sSql = sSql + "Report_Plan_Remark as 备注,"; }
                    }
                }
                sSql += "(select '<img  style=''cursor:pointer;'' src=''../images/edit.gif''> ') as 操作,";
                sSql = sSql.Insert(7, " Report_Plan_ID,");
                sSql = sSql.Substring(0, sSql.Length - 1);
                sSql += " from t_Report_Plan where 1=1";
                if (ExchangeDate != "")
                {
                    sSql += " and Report_Plan_ExchangeDate='" + ExchangeDate + "'";
                }
                if (ReportNO != "")
                {
                    sSql += " and Report_Plan_ReportNO='" + ReportNO + "'";
                }
                if (Session["IsBusiness"].ToString() == "True")
                {
                    sSql += " and Report_Plan_ProjectID in(SELECT Project_Hr_MainID   FROM [DB_EcSalseManage].[dbo].[t_Project_Hr] where Project_Hr_KDNameID in (" + Session["Building"].ToString() + "))";
                }
                    this.dgList1.DataKeyField = "Report_Plan_ID";
               
            }
            else if (ExchangeTypeID == "2")
            {
                CheckBoxList cbl = Page.FindControl("CheckBoxList2") as CheckBoxList;
                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    if (cbl.Items[i].Selected == true)
                    {
                        if (cbl.Items[i].Value.ToString() == "field_0009")
                        { sSql = sSql + "Report_SalesAlone_ExchangeDate as 日期,"; }
                        if (cbl.Items[i].Value.ToString() == "field_0010")
                        { sSql = sSql + "Report_SalesAlone_ReportNO as 成交编号,"; }
                        if (cbl.Items[i].Value.ToString() == "field_0011")
                        { sSql = sSql + "Report_SalesAlone_Subscribe as 认购书,"; }
                        if (cbl.Items[i].Value.ToString() == "field_0012")
                        { sSql = sSql + "Report_SalesAlone_Operator as 经办人,"; }
                    }
                }
                sSql += "Report_SalesAlone_SellUnit as 出售单位,Report_SalesAlone_Buyer as 购房人,Report_SalesAlone_Area as 购房面积,Report_SalesAlone_Price as 成交价,";
                sSql += "(select '<img  style=''cursor:pointer;'' src=''../images/edit.gif''> ') as 操作,";
                sSql = sSql.Insert(7, " Report_SalesAlone_ID,");
                sSql = sSql.Substring(0, sSql.Length - 1);
                sSql += " from t_Report_SalesAlone where 1=1";
                if (Session["IsBusiness"].ToString() == "True")
                {
                    sSql += " and Report_SalesAlone_ProjectID in(SELECT Project_Hr_MainID   FROM [DB_EcSalseManage].[dbo].[t_Project_Hr] where Project_Hr_KDNameID in (" + Session["Building"].ToString() + "))";
                }
                this.dgList1.DataKeyField = "Report_SalesAlone_ID";
            }
            else if (ExchangeTypeID == "3")
            {
                CheckBoxList cbl = Page.FindControl("CheckBoxList2") as CheckBoxList;
                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    if (cbl.Items[i].Selected == true)
                    {
                        if (cbl.Items[i].Value.ToString() == "field_0009")
                        { sSql = sSql + "Report_SalesLink_ExchangeDate as 日期,"; }
                        if (cbl.Items[i].Value.ToString() == "field_0010")
                        { sSql = sSql + "Report_SalesLink_ReportNO as 成交编号,"; }
                        if (cbl.Items[i].Value.ToString() == "field_0011")
                        { sSql = sSql + "Report_SalesLink_Subscribe as 认购书,"; }
                        if (cbl.Items[i].Value.ToString() == "field_0012")
                        { sSql = sSql + "Report_SalesLink_Operator as 经办人,"; }
                    }
                }
                sSql += "Report_SalesLink_SellUnit as 出售单位,Report_SalesLink_Buyer as 购房人,Report_SalesLink_Area as 购房面积,Report_SalesLink_Price as 成交价,";
                sSql += "(select '<img  style=''cursor:pointer;'' src=''../images/edit.gif''> ') as 操作,";
                sSql = sSql.Insert(7, " Report_SalesLink_ID,");
                sSql = sSql.Substring(0, sSql.Length - 1);
                sSql += " from t_Report_SalesLink where 1=1";
                if (Session["IsBusiness"].ToString() == "True")
                {
                    sSql += " and Report_SalesLink_ProjectID in(SELECT Project_Hr_MainID   FROM [DB_EcSalseManage].[dbo].[t_Project_Hr] where Project_Hr_KDNameID in (" + Session["Building"].ToString() + "))";
                }
                this.dgList1.DataKeyField = "Report_SalesLink_ID";
            }
            else if (ExchangeTypeID == "4")
            {

                sSql += "*,(select '<img  style=''cursor:pointer;'' src=''../images/edit.gif''> ') as 操作 from t_Report_BuyStandAlone";
                sSql += " where 1=1";
                if (Session["IsBusiness"].ToString() == "True")
                {
                    sSql += " and Report_BuyStandAlone_ProjectID in(SELECT Project_Hr_MainID   FROM [DB_EcSalseManage].[dbo].[t_Project_Hr] where Project_Hr_KDNameID in (" + Session["Building"].ToString() + "))";
                }
                this.dgList1.DataKeyField = "Report_BuyStandAlone_ID";
            }
            else if (ExchangeTypeID == "5")
            {

                sSql += "*,(select '<img  style=''cursor:pointer;'' src=''../images/edit.gif''> ') as 操作 from t_Report_Change";
                sSql += " where 1=1 and Report_Change_ExchangeTypeID=5";
                if (Session["IsBusiness"].ToString() == "True")
                {
                    sSql += " and Report_Change_ProjectID in(SELECT Project_Hr_MainID   FROM [DB_EcSalseManage].[dbo].[t_Project_Hr] where Project_Hr_KDNameID in (" + Session["Building"].ToString() + "))";
                }
                this.dgList1.DataKeyField = "Report_Change_ID";
            }
            else if (ExchangeTypeID == "6")
            {

                sSql += "*,(select '<img  style=''cursor:pointer;'' src=''../images/edit.gif''> ') as 操作 from t_Report_Change";
                sSql += " where 1=1 and Report_Change_ExchangeTypeID=6";
                if (Session["IsBusiness"].ToString() == "True")
                {
                    sSql += " and Report_Change_ProjectID in(SELECT Project_Hr_MainID   FROM [DB_EcSalseManage].[dbo].[t_Project_Hr] where Project_Hr_KDNameID in (" + Session["Building"].ToString() + "))";
                }
                this.dgList1.DataKeyField = "Report_Change_ID";
            }
            else if (ExchangeTypeID == "7")
            {

                sSql += "*,(select '<img  style=''cursor:pointer;'' src=''../images/edit.gif''> ') as 操作 from t_Report_Change_AreaUnit";
                sSql += " where 1=1 and Report_Change_AreaUnit_ExchangeTypeID=7";
                if (Session["IsBusiness"].ToString() == "True")
                {
                    sSql += " and Report_Change_AreaUnit_ProjectID in(SELECT Project_Hr_MainID   FROM [DB_EcSalseManage].[dbo].[t_Project_Hr] where Project_Hr_KDNameID in (" + Session["Building"].ToString() + "))";
                }
                this.dgList1.DataKeyField = "Report_Change_AreaUnit_ID";
            }
            else if (ExchangeTypeID == "8")
            {

                sSql += "*,(select '<img  style=''cursor:pointer;'' src=''../images/edit.gif''> ') as 操作 from t_Report_Change_AreaUnit";
                sSql += " where 1=1 and Report_Change_AreaUnit_ExchangeTypeID=8";
                if (Session["IsBusiness"].ToString() == "True")
                {
                    sSql += " and Report_Change_AreaUnit_ProjectID in(SELECT Project_Hr_MainID   FROM [DB_EcSalseManage].[dbo].[t_Project_Hr] where Project_Hr_KDNameID in (" + Session["Building"].ToString() + "))";
                }
                this.dgList1.DataKeyField = "Report_Change_AreaUnit_ID";
            }
            else if (ExchangeTypeID == "9")
            {

                sSql += "*,(select '<img  style=''cursor:pointer;'' src=''../images/edit.gif''> ') as 操作 from t_Report_Change_ReduceUnit";
                sSql += " where 1=1";
                if (Session["IsBusiness"].ToString() == "True")
                {
                    sSql += " and Report_Change_ReduceUnit_ProjectID in(SELECT Project_Hr_MainID   FROM [DB_EcSalseManage].[dbo].[t_Project_Hr] where Project_Hr_KDNameID in (" + Session["Building"].ToString() + "))";
                }
                this.dgList1.DataKeyField = "Report_Change_ReduceUnit_ID";
            }
            else if (ExchangeTypeID == "10")
            {
           
                sSql += "*,(select '<img  style=''cursor:pointer;'' src=''../images/edit.gif''> ') as 操作 from t_Report_ChangeOperater";
                sSql += " where 1=1";
                if (Session["IsBusiness"].ToString() == "True")
                {
                    sSql += " and Report_ChangeOperater_ProjectID in(SELECT Project_Hr_MainID   FROM [DB_EcSalseManage].[dbo].[t_Project_Hr] where Project_Hr_KDNameID in (" + Session["Building"].ToString() + "))";
                }
                this.dgList1.DataKeyField = "Report_ChangeOperater_ID";
            }

            DataAccess.Operate.DA_Report_Search_Inherit Report_Search_Inherit = new DataAccess.Operate.DA_Report_Search_Inherit();

            DataSet ds= Report_Search_Inherit.Search(sSql);
            this.dgList1.DataSource = ds.Tables[0];
            this.dgList1.DataBind();
        }




        protected void dgList1_DataBinding(object sender, EventArgs e)
        {

        }

        protected void dgList1_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            string datakey = this.dgList1.DataKeyField;

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            /*判断是否为正常项和交替项*/
            {
                if (datakey == "Report_Plan_ID")
                {
                    e.Item.Cells[e.Item.Cells.Count - 1].Attributes.Add("onClick", @"window.open('Report_Plan_Detail.aspx?id=" + this.dgList1.DataKeys[e.Item.ItemIndex] + "','','menubar=no,toolbar=no,location=no,directoris=no,status=yes,resizable=yes,scrollbars=yes','fullscreen');");
                    //e.Item.Attributes.Add("onClick", @"window.open('Report_Plan_Detail.aspx?id=" + this.dgList1.DataKeys[e.Item.ItemIndex] + "','','menubar=no,toolbar=no,location=no,directoris=no,status=yes,resizable=yes,scrollbars=yes','fullscreen');");
                }
                else if (datakey == "Report_SalesAlone_ID")
                {
                    e.Item.Cells[0].Visible = false;
                    e.Item.Cells[e.Item.Cells.Count - 1].Attributes.Add("onClick", @"window.open('Report_SalesAlone_Detail.aspx?id=" + this.dgList1.DataKeys[e.Item.ItemIndex] + "&typeid=" + e.Item.Cells[0].Text + "','','menubar=no,toolbar=no,location=no,directoris=no,status=yes,resizable=yes,scrollbars=yes','fullscreen');");
                }
                else if (datakey == "Report_SalesLink_ID")
                {
                    e.Item.Cells[0].Visible = false;
                    e.Item.Cells[e.Item.Cells.Count - 1].Attributes.Add("onClick", @"window.open('Report_SalesLink_Detail.aspx?id=" + this.dgList1.DataKeys[e.Item.ItemIndex] + "&typeid=" + e.Item.Cells[0].Text + "','','menubar=no,toolbar=no,location=no,directoris=no,status=yes,resizable=yes,scrollbars=yes','fullscreen');");
                }
                else if (datakey == "Report_BuyStandAlone_ID")
                {
                    e.Item.Cells[0].Visible = false;
                    e.Item.Cells[e.Item.Cells.Count - 1].Attributes.Add("onClick", @"window.open('Report_BuyStandAlone_Detail.aspx?id=" + this.dgList1.DataKeys[e.Item.ItemIndex] + "&typeid=" + e.Item.Cells[0].Text + "','','menubar=no,toolbar=no,location=no,directoris=no,status=yes,resizable=yes,scrollbars=yes','fullscreen');");
                }
                else if (datakey == "Report_Change_ID")
                {
                    e.Item.Cells[0].Visible = false;
                    e.Item.Cells[e.Item.Cells.Count - 1].Attributes.Add("onClick", @"window.open('Report_Change_Detail.aspx?id=" + this.dgList1.DataKeys[e.Item.ItemIndex] + "&typeid=" + this.ddlExchangeType.SelectedValue + "','','menubar=no,toolbar=no,location=no,directoris=no,status=yes,resizable=yes,scrollbars=yes','fullscreen');");
                }
                else if (datakey == "Report_Change_AreaUnit_ID")
                {
                    e.Item.Cells[0].Visible = false;
                    e.Item.Cells[e.Item.Cells.Count - 1].Attributes.Add("onClick", @"window.open('Report_Change_AreaUnit_Detail.aspx?id=" + this.dgList1.DataKeys[e.Item.ItemIndex] + "&typeid=" + this.ddlExchangeType.SelectedValue + "','','menubar=no,toolbar=no,location=no,directoris=no,status=yes,resizable=yes,scrollbars=yes','fullscreen');");
                }
                else if (datakey == "Report_Change_ReduceUnit_ID")
                {
                    e.Item.Cells[e.Item.Cells.Count - 1].Attributes.Add("onClick", @"window.open('Report_Change_ReduceUnit_Detail.aspx?id=" + this.dgList1.DataKeys[e.Item.ItemIndex] + "&typeid=" + e.Item.Cells[0].Text + "','','menubar=no,toolbar=no,location=no,directoris=no,status=yes,resizable=yes,scrollbars=yes','fullscreen');");
                }
                else if (datakey == "Report_ChangeOperater_ID")
                {
                    e.Item.Cells[e.Item.Cells.Count - 1].Attributes.Add("onClick", @"window.open('Report_ChangeOperater_Detail.aspx?id=" + this.dgList1.DataKeys[e.Item.ItemIndex] + "&typeid=" + e.Item.Cells[0].Text + "','','menubar=no,toolbar=no,location=no,directoris=no,status=yes,resizable=yes,scrollbars=yes','fullscreen');");
                }
                
         
                //*给正常项（即每一列)添加onmouseover事件*/
                //e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ///*给正常项（即每一列)添加onmouseout事件*/
                ///*以上两行实现变换背景色*/
                //((LinkButton)e.Item.FindControl("lbtnDel")).Attributes.Add("onClick", "return confirm('确定要删除吗？')");
                ///*给删除按钮添加单击事件，实现删除前提示*/
            }
            else
            {
                if (datakey == "Report_BuyStandAlone_ID" || datakey == "Report_Change_ID" || datakey == "Report_Change_AreaUnit_ID" || datakey == "Report_SalesAlone_ID" || datakey == "Report_SalesLink_ID")
                {
                    e.Item.Cells[0].Visible = false;
                }
            }
        }









    }
}
